﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RedSand.Web.Mvc;
using RedSand.Site.Models;

namespace RedSand.Site.Controllers
{
    public class ProductController : BaseController
    {
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Manager()
        {
            return View(new ManagerModel { Title = "Product manager", AttachTinyMce = true });
        }
        [AjaxOrChildAction]
        public ActionResult List(ProductSearchModel  searchModel, int page=1)
        {
            int Total = 0;
            ViewBag.RouteValues = searchModel;
            var list = RsDataManager.GetList<ProductInfo>("ProductSearch", searchModel, page, 10, ref Total);
            ViewBag.TotalRecord = Total;
            return PartialView(list);
        }

        [AjaxOrChildAction]
        public ActionResult LatestDeals()
        {
            return PartialView("LatestDeals", RsDataManager.GetList<ProductInfo>("ProductGetListCustom"));
        }

        [HttpGet]
        public ActionResult products(string alias, int page = 1)
        {
            int Total = 0;
            ViewBag.RouteValues = new { Alias = alias,Status = 1 };
            var list = RsDataManager.GetList<ProductInfo2>("ProductGetByAlias", new { Alias = alias, Status = 1 }, page, 10, ref Total);
            ViewBag.TotalRecord = Total;
            ViewBag.ParentID = RsDataManager.GetInfo<CategoryInfo>("CategoryGetByAlias", new { alias = alias }).CategoryID;
            return View("products",list);
        }

        [AjaxOrChildAction, HttpGet]
        public ActionResult Create()
        {
            return PartialView();
        }
        [AjaxOrChildAction, HttpPost]
        public ActionResult Create(ProductInfo info, string NewImage)
        {
            if (string.IsNullOrWhiteSpace(info.Details))
            {
                return AjaxMessage(Message.ContentEmpty);
            }
            if (string.IsNullOrWhiteSpace(NewImage))
            {
                return AjaxMessage("Please choose a image");
            }
            if (ModelState.IsValid)
            {
                info.Details = HtmlDecode(info.Details);
                if (!string.IsNullOrWhiteSpace(NewImage)) info.Image = SaveImage(null);
                int result = RsDataManager.Save("ProductSave", info);
                if (result > 0)
                {
                    AddMessage("Create success, please update properties for this product");
                    return RedirectToAction("Edit", new { id = result, create=true });
                }
                else if (result == -2)
                {
                    DeleteFile(info.Image);
                    return AjaxMessage("please choose another Alias");
                }
                else
                {
                    DeleteFile(info.Image);
                    return AjaxMessage(Message.CreateError);
                }
            }
            return AjaxModelError();
        }
        [AjaxOrChildAction, HttpGet]
        public ActionResult Edit(int id, bool create = false)
        {
            ViewBag.Create = create;
            return PartialView(RsDataManager.GetInfo<ProductInfo>("ProductGetInfo", "ProductID", id));
        }
        [AjaxOrChildAction, HttpPost]
        public ActionResult Edit(ProductInfo info, string NewImage)
        {
            if (string.IsNullOrWhiteSpace(info.Details))
            {
                return AjaxMessage(Message.ContentEmpty);
            }
            if (ModelState.IsValid)
            {
                info.Details = HtmlDecode(info.Details);
                if (!string.IsNullOrWhiteSpace(NewImage)) info.Image = SaveImage(null);
                int result = RsDataManager.ExecuteGetReturn("ProductSave", info);
                if (result > 0)
                {
                    AddMessage(Message.UpdateSuccess);
                    return RedirectToAction("List");
                }
                else if (result == -2)
                {
                    if (!string.IsNullOrWhiteSpace(NewImage)) DeleteFile(info.Image);
                    return AjaxMessage("please choose another Alias");
                }
                else
                {
                    if (!string.IsNullOrWhiteSpace(NewImage)) DeleteFile(info.Image);
                    return AjaxMessage(Message.UpdateError);
                }
            }
            return AjaxModelError();
        }
        [AjaxOrChildAction]
        public ActionResult Delete(int[] id)
        {
            if (id != null)
            {
                int count = id.Count();
                foreach (int i in id)
                {
                    ProductInfo info = RsDataManager.GetInfo<ProductInfo>("ProductGetInfo", "ProductID", i);
                    if (RsDataManager.Delete("ProductDelete", "ProductID", i))
                    {
                        DeleteFile(info.Image);
                        count--;
                    }
                }
                if (count > 0)
                {
                    AddMessage("Cannot delete " + count + " records");
                }
                else
                {
                    AddMessage(Message.DeleteSuccess);
                }
            }
            return RedirectToAction("List");
        }
        [AjaxOrChildAction, HttpGet]
        public ActionResult Properties(int id)
        {
            return PartialView(RsDataManager.GetList<SpecificationInfo>("ProductGetProperties", new { ProductID = id }));
        }
        [AjaxOrChildAction, HttpPost]
        public ActionResult Properties(int id, int[] PropertyID, string[] Value)
        {
            for(int i=0;i<PropertyID.Count();i++)
            {
                RsDataManager.Save("ProductSaveProperty", new { ProductID = id, PropertyID = PropertyID[i], Value = Value[i] });
            }
            return Json(true);
        }
    }
}
